package com.ruoyi.wechat.controller;

import cn.hutool.core.lang.Validator;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wechat.domain.WxUserDriver;
import com.ruoyi.wechat.service.IWxUserDriverService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 驾驶员列表Controller
 *
 * @author ruoyi
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/wechat/driver")
public class WxUserDriverController extends BaseController {
    @Autowired
    private IWxUserDriverService wxUserDriverService;

    /**
     * 查询驾驶员列表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WxUserDriver wxUserDriver) {
        startPage();
        List<WxUserDriver> list = wxUserDriverService.selectWxUserDriverList(wxUserDriver);
        return getDataTable(list);
    }

    /**
     * 导出驾驶员列表列表
     */
    @Log(title = "驾驶员列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxUserDriver wxUserDriver) {
        List<WxUserDriver> list = wxUserDriverService.selectWxUserDriverList(wxUserDriver);
        ExcelUtil<WxUserDriver> util = new ExcelUtil<WxUserDriver>(WxUserDriver.class);
        util.exportExcel(response, list, "驾驶员列表数据");
    }

    /**
     * 获取驾驶员列表详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wxUserDriverService.selectWxUserDriverById(id));
    }

    /**
     * 新增驾驶员列表
     */
    @Log(title = "驾驶员列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxUserDriver wxUserDriver) {
        if (!Validator.isMobile(wxUserDriver.getPhone())) {
            return error("手机号码不正确！");
        }
        if (!Validator.isCitizenId(wxUserDriver.getSfcode())) {
            return error("身份证号码不正确！");
        }
        WxUserDriver sunUserDriver = new WxUserDriver();
        sunUserDriver.setSfcode(sunUserDriver.getSfcode());
        sunUserDriver.setWid(wxUserDriver.getWid());
        List<WxUserDriver> list = wxUserDriverService.selectWxUserDriverList(sunUserDriver);
        if (ObjectUtils.isNotEmpty(list)) {
            return AjaxResult.warn("身份证号码已存在！,默认使用已存在驾驶员",list.get(0));
        }
        wxUserDriver.setCreateTime(DateUtils.getNowDate());

        return toAjax(wxUserDriverService.insertWxUserDriver(wxUserDriver));
    }

    /**
     * 修改驾驶员列表
     */
    @Log(title = "驾驶员列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxUserDriver wxUserDriver) {
        return toAjax(wxUserDriverService.updateWxUserDriver(wxUserDriver));
    }

    /**
     * 删除驾驶员列表
     */
    @Log(title = "驾驶员列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wxUserDriverService.deleteWxUserDriverByIds(ids));
    }
}
